var express = require('express');
var app = express();
var path = require('path');
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static(path.join(__dirname, 'views')));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
  res.sendFile('/index.html');
});

var users = {}
io.on('connection', function(socket){
  socket.emit('hello', 'hello, your sessionId is ' + socket.id)
  socket.on('join', function(user){
  	if(!users[socket.id]){
  		users[socket.id] = user
  	}
  	io.emit('joined', true)
  	io.emit('users', users)
  })
  socket.on('send', function(data){
  	io.emit('messages', data)
  })
  socket.on('disconnect', function () {
    users[socket.id] = null
    io.emit('users', users)
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});